package com.bear.sgg.springboot_07_web_jsp.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @ClassNmae MyLocaleResolver
 * @Description 区域解析器（解析区域信息实现国际化）
 * @Author bear
 * @Date 2020/3/9 0009 下午 3:39
 */
public class MyLocaleResolver implements LocaleResolver {

    public MyLocaleResolver() {
        super();
    }

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lang = request.getParameter("lang");
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(lang)) {       //存在区域信息则用该区域信息进行国际化，否则使用默认的国际化信息
            String param[] = lang.split("_");
            locale = new Locale(param[0], param[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
